package com.gears_software.sxdc.dccx.ui.activity;

import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.TextView;


import com.facebook.drawee.view.SimpleDraweeView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IVehicleFinder;
import com.gears_software.sxdc.dccx.data.finder.RequestUrl;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.VehicleModel;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.WindowFeature;
import org.androidannotations.rest.spring.annotations.RestService;

import java.text.SimpleDateFormat;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by chezhenzhen on 2017/7/1.
 */

@EActivity(R.layout.activity_xingshizheng_authentication)
@WindowFeature(Window.FEATURE_NO_TITLE)
public class XingShiZhengAuthenticationActivity extends AppCompatActivity {

    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView txt_state;
    @ViewById
    TextView tv_tongguo;
    @ViewById
    SimpleDraweeView img_card1;
    @ViewById
    SimpleDraweeView img_card2;
    @ViewById
    SimpleDraweeView img_card3;
    @ViewById
    TextView txt_registerdate;
    @ViewById
    TextView txt_carmodel;
    @ViewById
    TextView txt_carowner;
    @ViewById
    TextView txt_carnumber;
    @RestService
    IVehicleFinder vehicleFinder;
    @Bean
    GearsErrorHandler errorHandler;
    @Extra
    String id;

    @AfterViews
    void created() {
        initIconFont();
        StatusBarCompat.setStatusBarColor(this,ContextCompat.getColor(this, R.color.colorActionbar));

        vehicleFinder.setRestErrorHandler(errorHandler);
        getVehicleInfo();

    }

    void initIconFont() {
        actionbar_title.setText("车主认证");
        back_icon.setVisibility(View.VISIBLE);
        back_icon.setText(R.string.back_icon);
        back_icon.setTypeface(app.iconfont);
        right_icon.setVisibility(View.GONE);
        tv_tongguo.setTypeface(app.iconfont);
    }

    @Background
    void getVehicleInfo() {
        if (id != null || !id.equals("")) {
            ResultModel<VehicleModel> res = vehicleFinder.getVehicleInfo(id);
            showVehicleInfoResult(res);
        }
    }

    @UiThread
    void showVehicleInfoResult(ResultModel<VehicleModel> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    VehicleModel vehicleModel = res.getData();
                    if (vehicleModel != null) {
                        if (vehicleModel.getCarauditstate().equals("2")) {
                            tv_tongguo.setTextColor(ContextCompat.getColor(this, R.color.colorFontOrange));
                            txt_state.setText("认证中");
                            txt_state.setTextColor(ContextCompat.getColor(this,R.color.colorFontOrange));
                            txt_state.setBackgroundResource(R.drawable.rounded_rectangle_orange);
                        } else {
                            tv_tongguo.setTextColor(ContextCompat.getColor(this, R.color.colorGreen));
                            txt_state.setText("认证成功");
                            txt_state.setTextColor(ContextCompat.getColor(this,R.color.colorGreen));
                            txt_state.setBackgroundResource(R.drawable.rounded_rectangle_green);
                        }
                        Uri uri1 = Uri.parse(vehicleModel.getVehicellicenseid1() + RequestUrl.card);
                        Uri uri2 = Uri.parse(vehicleModel.getVehicellicenseid2() + RequestUrl.card);
                        Uri uri3 = Uri.parse(vehicleModel.getMancarphone2() + RequestUrl.card);
                        img_card1.setImageURI(uri1);
                        img_card2.setImageURI(uri2);
                        img_card3.setImageURI(uri3);
                        txt_carnumber.setText(vehicleModel.getVehicleno());
                        txt_carmodel.setText(vehicleModel.getCarmodel());
                        txt_carowner.setText(vehicleModel.getOwnername());
                        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
                        txt_registerdate.setText(sf.format(vehicleModel.getCertifydatea()));

                    }
                }
            }
        }
    }

    @Click
    void back_icon() {
        this.finish();
    }
}
